package com.pajk.user.service.credit;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

import com.pajk.user.model.CreditRule;
import com.pajk.user.util.DateUtil;

/**
 * Created by chenhaihua on 14-4-28.
 */
public class CreditRuleWrap {
    private static final String keySeparator = "_";
    private int                 credit;
    private char               policy;
    private int                 times;
    private String              actionCode;

    private String              tairKey;
    private Date                expiredDate;
    private boolean             isOverTimes;

    public void init(long userId, Date actionTime) {
        StringBuilder keyBuilder = new StringBuilder(Long.toString(userId)).append(keySeparator)
            .append(actionCode);
        switch (policy) {
            case 'D'://天
                expiredDate = DateUtils.addDays(DateUtils.truncate(actionTime, Calendar.DATE), 1);
                keyBuilder.append(keySeparator).append(
                    DateUtil.format(expiredDate, DateUtil.DTSHORT));
                break;
            case 'W'://周
                expiredDate = DateUtils.addWeeks(DateUtil.getFirstDayOfWeek(actionTime), 1);
                keyBuilder.append(keySeparator)
                    .append(DateUtil.format(expiredDate, DateUtil.DTYEAR)).append(keySeparator)
                    .append(DateUtil.getWeekOfYear(expiredDate));
                break;
            case 'M'://月
                expiredDate = DateUtils
                    .addMonths(DateUtils.truncate(actionTime, Calendar.MONTH), 1);
                keyBuilder.append(keySeparator).append(
                    DateUtil.format(expiredDate, DateUtil.DTMONTH));
                break;
            case 'U':
            default:
        }
        tairKey = keyBuilder.toString();
    }

    public int getCredit() {
        return credit;
    }

    public char getPolicy() {
        return policy;
    }

    public int getTimes() {
        return times;
    }

    public String getTairKey() {
        return tairKey;
    }

    public CreditRuleWrap(String actionCode, char policy, int times, int credit) {
        this.credit = credit;
        this.policy = policy;
        this.times = times;
        this.actionCode = actionCode;
        this.isOverTimes = false;
    }

    public CreditRuleWrap(CreditRule creditRule) {
        this.credit = creditRule.getCredit();
        this.policy = creditRule.getCreditRuleEnum().name().charAt(0);
        this.times = creditRule.getTimes();
        this.actionCode = creditRule.getActionCode();
        this.isOverTimes = false;
    }
    
    public Date getExpiredDate() {
        return expiredDate;
    }

    public boolean isOverTimes() {
        return isOverTimes;
    }

    public void setOverTimes(boolean isOverTimes) {
        this.isOverTimes = isOverTimes;
    }

    @Override
    public String toString() {
        return "CreditRule{" + "credit=" + credit + ", policy=" + policy + ", times=" + times
               + ", tairKey='" + tairKey + '\'' + '}';
    }
}
